/*
 * This file (AxParameter.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package artx.parameters;

import artx.objects.AxObject;
import artx.utils.AxLog;

/**
 * Basic (abstract) parameter class.
 * 
 * @author Xavier Gouchet
 * 
 */
public abstract class AxParameter extends AxObject {

	// DISCUSS add a hidden setting?
	/**
	 * String value of the parameter, only generic way to comunicate with a
	 * parameter.
	 */
	protected String strValue;

	/**
	 * Default string value of the parameter, used to reset the parameter if
	 * needed.
	 */
	protected String strDefault;

	/** Type of the parameter, used for gui. */
	protected String strType;

	/**
	 * Standard constructor.
	 * 
	 * @param name
	 *            the parameter's name
	 * @param type
	 *            the parameter's type
	 */
	public AxParameter(String name, String type) {
		super(-1, name);
		this.strValue = "";
		this.strDefault = "";
		this.strType = type.toLowerCase();
		this.UpdateValue();
	}

	/**
	 * Standard constructor.
	 * 
	 * @param name
	 *            the parameter's name
	 * @param reset
	 *            the parameter's default value
	 * @param type
	 *            the parameter's type
	 */
	public AxParameter(String name, String type, String reset) {
		super(-1, name);
		if (reset != null) {
			this.strDefault = new String(reset);
			this.strValue = new String(reset);
			this.strType = type.toLowerCase();
		} else {
			this.strValue = "";
			this.strDefault = "";
			this.strType = type.toLowerCase();
		}
		this.UpdateValue();
	}

	/**
	 * Sets the parameter using a string.
	 * 
	 * <p>
	 * Each subclass of AxParameter defines a way to convert a string to a
	 * correct value, using the {@link #UpdateValue()} method.
	 * </p>
	 * 
	 * @param s
	 *            the new string value of the parameter
	 * @return boolean whether the conversion was successfull
	 */
	public final boolean SetValue(String s) {
		AxLog.Debug("AxParameter::SetValue =>> setting parameter value ["
				+ this.NAME + ";\"" + s + "\"]");
		if ((s == null) || (s.isEmpty())) {
			this.strValue = "";
		} else {
			this.strValue = new String(s);
		}
		return this.UpdateValue();
	}

	/**
	 * Converts the string value into a custom typed value.
	 * 
	 * @return boolean whether the conversion was successfull
	 */
	public abstract boolean UpdateValue();

	/**
	 * Resets the parameter to its default value.
	 */
	public final void ResetValue() {
		this.strValue = new String(this.strDefault);
		this.UpdateValue();
	}

	/**
	 * Gets the string value of the parameter.
	 * 
	 * @return the current string value of the parameter
	 */
	public final String GetStringValue() {
		return new String(this.strValue);
	}

	/**
	 * Get the default string value of the parameter.
	 * 
	 * @return the default string value of the parameter
	 */
	public final String GetResetValue() {
		return new String(this.strDefault);
	}

	/**
	 * Gets the string type of the parameter.
	 * 
	 * @return the type of the parameter
	 */
	public final String GetType() {
		return new String(this.strType);
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "\"" + this.NAME + "\" = " + this.strValue + " [" + this.strType
				+ "]";
	}
}